# 对 flask_sam 的测试函数
# test 
img = './tmp.jpg'
rect = (10,20,50,70)
import requests
import json

# 测试用图片和矩形框
img_path = img  # './tmp.jpg'
rect_dict = {
    "x": rect[0],
    "y": rect[1],
    "width": rect[2],
    "height": rect[3]
}

url = "http://127.0.0.1:20022/process"

with open(img_path, "rb") as f:
    files = {
        "image": f
    }
    data = {
        "rect": json.dumps(rect_dict)
    }
    response = requests.post(url, files=files, data=data)

# 保存返回的图片
if response.status_code == 200:
    with open("result_mask.png", "wb") as out:
        from PIL import Image
        from io import BytesIO
        img = Image.open(BytesIO(response.content))
        img.save(out, format="PNG")
    print("结果已保存到 result_mask.png")
else:
    print("请求失败，状态码：", response.status_code)
    print("返回内容：", response.text)
